package ar.org.sstuyv

import grails.converters.JSON

import org.springframework.dao.DataIntegrityViolationException

class InformesUrbanisticosController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [informesUrbanisticosInstanceList: InformesUrbanisticos.list(params), informesUrbanisticosInstanceTotal: InformesUrbanisticos.count()]
    }

    def create() {
        [informesUrbanisticosInstance: new InformesUrbanisticos(params)]
    }
	
	def edit(Long id) {
		[informesUrbanisticosInstance: InformesUrbanisticos.get(id)]
	}
	
    def save() {
		def idNomenclaturaCatastral = params.nomenclaturaCatastralId
		NomenclaturaCatastral nc = NomenclaturaCatastral.findById(idNomenclaturaCatastral)
		params.nomenclaturaCatastral = nc
        def informesUrbanisticosInstance = new InformesUrbanisticos(params)
//		informesUrbanisticosInstance.setNomenclaturaCatastral(nc)
        if (!informesUrbanisticosInstance.save(flush: true)) {
            render(view: "create", model: [informesUrbanisticosInstance: informesUrbanisticosInstance])
            return
        }

		
        flash.message = message(code: 'default.created.message', args: [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos'), informesUrbanisticosInstance.id])
        redirect(action: "list", id: informesUrbanisticosInstance.id)
    }

    def show(Long id) {
        def informesUrbanisticosInstance = InformesUrbanisticos.get(id)
        if (!informesUrbanisticosInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos'), id])
            redirect(action: "list")
            return
        }

        [informesUrbanisticosInstance: informesUrbanisticosInstance]
    }

//    def edit(Long id) {
//        def informesUrbanisticosInstance = InformesUrbanisticos.get(id)
//        if (!informesUrbanisticosInstance) {
//            flash.message = message(code: 'default.not.found.message', args: [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos'), id])
//            redirect(action: "list")
//            return
//        }
//
//        [informesUrbanisticosInstance: informesUrbanisticosInstance]
//    }

	def update() {
		def informesUrbanisticosInstance = InformesUrbanisticos.get(params.id)
		informesUrbanisticosInstance.properties = params
		
		informesUrbanisticosInstance.save()		
		
		render ""
	}
		
	
//    def update(Long id, Long version) {
//        def informesUrbanisticosInstance = InformesUrbanisticos.get(id)
//        if (!informesUrbanisticosInstance) {
//            flash.message = message(code: 'default.not.found.message', args: [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos'), id])
//            redirect(action: "list")
//            return
//        }
//
//        if (version != null) {
//            if (informesUrbanisticosInstance.version > version) {
//                informesUrbanisticosInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
//                          [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos')] as Object[],
//                          "Another user has updated this InformesUrbanisticos while you were editing")
//                render(view: "edit", model: [informesUrbanisticosInstance: informesUrbanisticosInstance])
//                return
//            }
//        }
//
//        informesUrbanisticosInstance.properties = params
//
//        if (!informesUrbanisticosInstance.save(flush: true)) {
//            render(view: "edit", model: [informesUrbanisticosInstance: informesUrbanisticosInstance])
//            return
//        }
//
//        flash.message = message(code: 'default.updated.message', args: [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos'), informesUrbanisticosInstance.id])
//        redirect(action: "show", id: informesUrbanisticosInstance.id)
//		
//		render ""
//    }

    def delete(Long id) {
        def informesUrbanisticosInstance = InformesUrbanisticos.get(id)
        if (!informesUrbanisticosInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos'), id])
            redirect(action: "list")
            return
        }

        try {
            informesUrbanisticosInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'informesUrbanisticos.label', default: 'InformesUrbanisticos'), id])
            redirect(action: "show", id: id)
        }
    }
	
	def saveAjax() {
		def rs
		def decretoPromulgatorio = params.decretoPromulgatorio
		Partido partido = Partido.findById(params.partido.id)
		//Me fijo si ya existe
		InformesUrbanisticos informeUrbanistico = InformesUrbanisticos.findByPartidoAndDecretoPromulgatorio(partido, params.decretoPromulgatorio)
		if (!informeUrbanistico) {
			def informesUrbanisticosInstance = new InformesUrbanisticos(params)
			informesUrbanisticosInstance.save(failOnError:true, flush: true)
			rs = 'alta'
		} else {
			rs = 'existente'
		}
			
		def jsonData = [rs: rs]
		render jsonData as JSON

	}
	
	def updateAjax() {
		def rs = 'modificacion'
		def decretoPromulgatorio = params.decretoPromulgatorio
		Partido partido = Partido.findById(params.partido.id)
		InformesUrbanisticos informeUrbanistico = InformesUrbanisticos.findByPartidoAndDecretoPromulgatorio(partido, params.decretoPromulgatorio)
		informeUrbanistico.properties = params
		informeUrbanistico.save(flush: true)
		def jsonData = [rs: rs]
		render jsonData as JSON
	}
	  
}
